package com.xxx.meter.ui.meter.reading.selection;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.xxx.meter.R;
import com.xxx.meter.base.BaseActivity;
import com.xxx.meter.base.BaseDialog;
import com.xxx.meter.model.utils.ButtonUtil;
import com.xxx.meter.model.utils.ToastUtil;
import com.xxx.meter.ui.main.custom.MyLayoutManager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class MeterReadingSelectionWindow extends BaseDialog {

    @BindView(R.id.window_patrol_selection_linear)
    LinearLayout mLinear;
    @BindView(R.id.window_patrol_selection_selection)
    LinearLayout mSelection;

    private List<MyLayoutManager> layoutManagers = new ArrayList<>();
    private List<MeterReadingSelectionAdapter> adapters = new ArrayList<>();

    private Callback callback;
    private double value;

    public static MeterReadingSelectionWindow getInstance(BaseActivity activity, double value, Callback callback) {
        return new MeterReadingSelectionWindow(activity, value, callback);
    }

    private MeterReadingSelectionWindow(Context context, double value, Callback callback) {
        super(context, 0.95, false);
        this.value = value;
        this.callback = callback;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.window_patrol_selection;
    }

    @Override
    protected void initData() {
        List<String> list = new ArrayList<>();
        String s = String.valueOf(value);
        String[] split = s.split("\\.");

        //确认下数值前的限制 多加0
        if (split[0].length() == 1) {
            s = "0" + s;
        }

        //确认下位数后的限制 多加0
        if (split[1].length() == 1) {
            s = s + "0";
        }

        for (int i = 0; i < s.length(); i++) {
            list.add(String.valueOf(s.charAt(i)));
        }

        int length = s.length();
        mLinear.setOrientation(LinearLayout.HORIZONTAL);

        for (int i = 0; i < length; i++) {
            List<String> list1 = initNumberList();
            RecyclerView recyclerView = new RecyclerView(getContext());
            recyclerView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1.0f));
            MyLayoutManager myLayoutManager = new MyLayoutManager(getContext(), LinearLayoutManager.VERTICAL);
            layoutManagers.add(myLayoutManager);
            int number;
            if (String.valueOf(s.charAt(i)).equals(".")) {
                number = 10;
            } else {
                number = Integer.parseInt(String.valueOf(s.charAt(i)));
            }
            myLayoutManager.scrollToPositionWithOffset(list1.size() / 2 + number - 4, 0);
            recyclerView.setLayoutManager(myLayoutManager);
            MeterReadingSelectionAdapter mAdapter = new MeterReadingSelectionAdapter(list1);
            adapters.add(mAdapter);
            recyclerView.setAdapter(mAdapter);
            mLinear.addView(recyclerView);
        }
    }

    private List<String> initNumberList() {
        List<String> list = new ArrayList<>();
        for (int j = 0; j < 10; j++) {
            for (int i = 0; i <= 10; i++) {
                if (i == 10) {
                    list.add(".");
                } else {
                    list.add(String.valueOf(i));
                }
            }
        }
        return list;
    }

    @OnClick({R.id.window_patrol_selection_finish, R.id.window_patrol_selection_btn})
    public void OnClick(View view) {
        if (ButtonUtil.isFastClick()) return;
        switch (view.getId()) {
            case R.id.window_patrol_selection_finish:
                dismiss();
                break;
            case R.id.window_patrol_selection_btn:
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < layoutManagers.size(); i++) {
                    int firstVisibleItemPosition = layoutManagers.get(i).findFirstVisibleItemPosition() + 4;
                    String s = adapters.get(i).getData().get(firstVisibleItemPosition);
                    sb.append(s);
                }
                try {
                    value = Double.parseDouble(sb.toString());
                    if (value > 10000){
                        ToastUtil.showToast("请确认数值是否输入正确");
                        return;
                    }
                } catch (Exception e) {
                    ToastUtil.showToast("请确认数值是否输入正确");
                    return;
                }
                if (callback != null) callback.callback(value);
                break;
        }
    }

    public interface Callback {
        void callback(double value);
    }
}
